package com.hankol.community.respository

import androidx.lifecycle.MutableLiveData
import com.baselibrary.BuildConfig
import com.baselibrary.base.BasicListBean
import com.baselibrary.base.mvvm.BaseApi
import com.baselibrary.bean.*
import com.baselibrary.extension.checkValue
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.utils.AppUtils
import com.baselibrary.utils.CommonUtil
import com.baselibrary.utils.HKLogUtils
import com.baselibrary.utils.SpUtils
import com.hankol.community.api.CommunityApi
import com.hankol.community.bean.*
import com.okhttp.callbacks.Callback
import com.okhttp.comment.CommentBean
import com.okhttp.comment.CommentConstant
import com.okhttp.utils.OkHttpUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class CommunityRequestManager {
    companion object {
        @Volatile
        private var instance: CommunityRequestManager? = null

        fun getInstance(): CommunityRequestManager {
            return instance ?: synchronized(this) {
                instance ?: CommunityRequestManager().also { instance = it }
            }
        }
    }

    /*搜索我的好友*/
    fun reqSearchFriend(keyWord: String, mld: MutableLiveData<List<FriendListBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).reqSearchFriend(it, keyWord)
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<List<FriendListBean>>(this) {
                    override fun onSuccess(t: List<FriendListBean>, json: String) {
                        mld.value = t
                    }

                    override fun onError(e: Throwable) {
                        mld.value = emptyList()
                        super.onError(e)
                    }
                })
        }
    }

    /**
     * 推荐视频
     */
    fun reqVideoDetails(
        listData: MutableLiveData<VideoReadBean.DataBeanX>,
        cursor: String,
        currentId: Long,
        btype: Long,
        bid: String,
        deviceId: String
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqVideoDetails(it, cursor, currentId, btype, bid, deviceId)
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<VideoReadBean.DataBeanX>(this) {
                    override fun onSuccess(t: VideoReadBean.DataBeanX, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        listData.value = null
                        super.onError(e)
                    }
                })
        }
    }

    /**
     * 推荐视频
     */
    fun reqVideoDetails(
        listData: MutableLiveData<VideoReadBean.DataBeanX>,
        cursor: String,
        currentId: Long,
        btype: Long,
        deviceId: String
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqVideoDetails(it, cursor, currentId, btype, deviceId)
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<VideoReadBean.DataBeanX>(this) {
                    override fun onSuccess(t: VideoReadBean.DataBeanX, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        listData.value = null
                        super.onError(e)
                    }
                })
        }
    }

    /**
     *  视频页 列表
     */
    fun reqVideoPageList(listData: MutableLiveData<VideoReadBean.DataBeanX>, cursor: String) {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqVideoPageList(cursor)
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<VideoReadBean.DataBeanX>(this) {
                    override fun onSuccess(t: VideoReadBean.DataBeanX, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        listData.value = null
                        super.onError(e)
                    }
                })
    }

    /**
     * 标记推荐视频已读
     */
    fun reqVideoHistoryAdd(listData: MutableLiveData<Any>, busiId: Long) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqVideoHistoryAdd(it, busiId, 2)?.subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<Any>(this) {
                    override fun onSuccess(t: Any, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        listData.value = null
                        super.onError(e)
                    }
                })
        }
    }


    /**
     * @param topCommentId 置顶评论的id。。默认传0就行
     */
    fun reqCommentList(
        commentListLiveData: MutableLiveData<CommentBean.DataBean>,
        page: Int,
        type: Int,
        typeId: Long,
        pid: Long,
        topCommentId: Long
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).reqCommentList(
                it,
                CommentConstant.COMMENT_LOAD_COUNT,
                type,
                typeId,
                page,
                pid,
                topCommentId
            ).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<CommentBean.DataBean>(this) {
                    override fun onSuccess(
                        t: CommentBean.DataBean,
                        json: String
                    ) {
                        commentListLiveData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        commentListLiveData.value = null
                    }
                })
        }
    }

    fun reqDynamicDetails(
        commentListLiveData: MutableLiveData<TimelineInfoData.ClientTimeline>,
        dynamicId: Long
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqDynamicDetails(it, dynamicId).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<TimelineInfoData.ClientTimeline>(this) {
                    override fun onSuccess(t: TimelineInfoData.ClientTimeline, json: String) {
                        commentListLiveData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        commentListLiveData.value = null
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        commentListLiveData.value = null
                    }
                })
        }
    }

    /*首页Banner和热门话题*/
    fun requestBannerAndHotTags(listData: MutableLiveData<HotBannerData.DataDTO>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).requestBannerAndHotTags(it)
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<HotBannerData.DataDTO>(this) {
                    override fun onSuccess(t: HotBannerData.DataDTO, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        listData.value = null
                    }
                })
        }
    }

    /*频道Banner和热门话题*/
    fun requestChannelBannerAndHotTags(
        channelId: Int,
        listData: MutableLiveData<HotBannerData.DataDTO>
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .requestChannelBannerAndHotTags(it, channelId)?.subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<HotBannerData.DataDTO>(this) {
                    override fun onSuccess(t: HotBannerData.DataDTO, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        listData.value = null
                    }
                })
        }
    }

    fun requestTopicDetail(id: String, listData: MutableLiveData<TopicDetailsBean.DataBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).requestTopicDetail(it, id)
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<TopicDetailsBean.DataBean>(this) {
                    override fun onSuccess(t: TopicDetailsBean.DataBean, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        listData.value = null
                    }
                })
        }
    }

    fun requestSubscribeLive(id: String, apiResponse: APIResponse<String>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).requestSubscribeLive(it, id)
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(apiResponse)
        }
    }

    fun requestCancelSubscribeLive(id: String, apiResponse: APIResponse<String>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .requestCancelSubscribeLive(it, id)?.subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())?.subscribe(apiResponse)
        }
    }
//根据话题分类id 获取话题列表 type 0选择话题 1全部话题
    fun reqSearchTopic(
        tagCategoryId: Long,
        type: Int,
        listData: MutableLiveData<List<ReleaseTopicBean.DataBean>>
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqSearchTopic(it, tagCategoryId, Int.MAX_VALUE.toLong(),type)
                .subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<List<ReleaseTopicBean.DataBean>>(this) {
                    override fun onSuccess(t: List<ReleaseTopicBean.DataBean>, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        listData.value = null
                    }
                })
        }
    }


    fun requestChannelList(listData: MutableLiveData<List<HomeChannelBean>>) {
        MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
            .requestChannelList(SpUtils.getInstance().loadUserBean?.data?.token.checkValue())
            ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe(object : APIResponse<List<HomeChannelBean>>(this) {
                override fun onSuccess(t: List<HomeChannelBean>, json: String) {
                    listData.value = t
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    listData.value = null
                }
            })
    }

    fun requestTopicGoodUser(page: Int, id: String, listData: MutableLiveData<GoodUserBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .requestTopicGoodUser(it, page, 10, id)?.subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<GoodUserBean>(this) {
                    override fun onSuccess(t: GoodUserBean, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        listData.value = null
                    }
                })
        }
    }

    /**
     * 专栏分页列表
     */
    fun reqColumnListData(
        page: Int,
        followLD: MutableLiveData<BasicListBean<SquareSpecialColumnBean>>
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqColumnListNewData(it, page, 20).subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<BasicListBean<SquareSpecialColumnBean>>(this) {
                    override fun onSuccess(
                        result: BasicListBean<SquareSpecialColumnBean>,
                        json: String?
                    ) {
                        followLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followLD.value = null
                    }

                })
        }
    }

    /**
     * 百科Banner
     */
    fun reqWikiBanner(call: Callback<WikiHomeAmusingMoreRespWiki>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            val getBuilder = OkHttpUtils.get()
            getBuilder.addHeader("token", it).
            addHeader("platform","android").
            addHeader("platVersion", AppUtils.getVersionName())
            getBuilder.url(
                "${BuildConfig.URL_HANKEAPI}wkapi/essay/v2/list4recommendList"
            ).build().execute(call)
        }
    }

    /**
     * 百科分页列表
     */
    fun reqWikiList(page: Int, call: Callback<WikiHomeAmusingMoreRespWiki>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            val getBuilder = OkHttpUtils.get()
            getBuilder.addHeader("token", it)
            val mParamsMap = HashMap<String, String>()
            mParamsMap["pageNum"] = page.toString()
            getBuilder.url(
                "${BuildConfig.URL_HANKEAPI}wkapi/essay/v2/findEssayList4itemPage${
                    CommonUtil.getParams(
                        mParamsMap
                    )
                }"
            ).build().execute(call)
        }
    }

    /**
     * 讲坛分类  分页列表
     */
    fun reqForumClassifyList(
        page: Int,
        followLD: MutableLiveData<BasicListBean<ForumClassifyListBean>>
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqForumClassifyList(it, page, 20).subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<BasicListBean<ForumClassifyListBean>>(this) {
                    override fun onSuccess(
                        result: BasicListBean<ForumClassifyListBean>,
                        json: String?
                    ) {
                        followLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followLD.value = BasicListBean(emptyList())
                    }
                })
        }
    }


    /**
     * 某个分类下 的 讲坛  分页列表
     */
    fun reqForumList(
        ctId: Int,
        page: Int,
        followLD: MutableLiveData<BasicListBean<ForumListBean>>
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqForumList(it, ctId, page, 10).subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<BasicListBean<ForumListBean>>(this) {
                    override fun onSuccess(result: BasicListBean<ForumListBean>, json: String?) {
                        followLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followLD.value = null
                    }

                })
        }
    }

    /**
     * 专栏详情  分页列表
     */
    fun reqColumnDetailList(
        page: Int,
        cid: Int,
        followLD: MutableLiveData<BasicListBean<ColumnDetailsListBean>>
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqColumnDetailList(it, cid, page, 10).subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<BasicListBean<ColumnDetailsListBean>>(this) {
                    override fun onSuccess(
                        result: BasicListBean<ColumnDetailsListBean>,
                        json: String?
                    ) {
                        followLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followLD.value = null
                    }

                })
        }
    }

    /**
     * 关注页默认动态列表
     */
    fun reqFollowDynamicList(page: Int, followLD: MutableLiveData<FollowDynamicBean.DataBeanX>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqFollowDynamicList(it, page, 10)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<FollowDynamicBean.DataBeanX>(this) {
                    override fun onSuccess(result: FollowDynamicBean.DataBeanX, json: String?) {
                        followLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followLD.value = null
                    }

                })
        }
    }

    /**
     *文章详情
     */
    fun reqArticleDetail(aid: Long, followLD: MutableLiveData<ArticleDetailBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).reqArticleDetail(it, aid)
                .subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<ArticleDetailBean>(this) {
                    override fun onSuccess(result: ArticleDetailBean, json: String?) {
                        followLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followLD.value = null
                    }

                })
        }
    }

    /**
     *文章详情 阅读数+1
     */
    fun reqArticleDetailRedNumPlus(aid: Long, followLD: MutableLiveData<Boolean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqArticleDetailRedNumPlus(it, aid).subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<Boolean>(this) {
                    override fun onSuccess(result: Boolean, json: String?) {
                        followLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followLD.value = null
                    }

                })
        }
    }

    /**
     *专栏详情
     */
    fun reqColumnDetailData(aid: Int, followLD: MutableLiveData<ColumnDetailBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).reqColumnDetailData(it, aid)
                .subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<ColumnDetailBean>(this) {
                    override fun onSuccess(result: ColumnDetailBean, json: String?) {
                        followLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followLD.value = null
                    }

                })
        }
    }

    /**
     *讲坛分类详情
     */
    fun reqForumClassDetail(aid: Int, followLD: MutableLiveData<ForumClassifyInfoBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).reqForumClassDetail(it, aid)
                .subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<ForumClassifyInfoBean>(this) {
                    override fun onSuccess(result: ForumClassifyInfoBean, json: String?) {
                        followLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followLD.value = null
                    }

                })
        }
    }

    fun reqFollowList(followUsersLD: MutableLiveData<FollowUserBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqFollowUsers(it).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<FollowUserBean>(this) {
                    override fun onSuccess(result: FollowUserBean, json: String?) {
                        followUsersLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followUsersLD.value = null
                    }

                })
        }
    }

    fun reqAllFollowList(
        followUsersLD: MutableLiveData<BasicListBean<FollowUsersBeanItem>>,
        page: Int
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqMyFollow(it, "", 1, page, 10)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<BasicListBean<FollowUsersBeanItem>>(this) {
                    override fun onSuccess(
                        result: BasicListBean<FollowUsersBeanItem>,
                        json: String?
                    ) {
                        followUsersLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followUsersLD.value = null
                    }

                })
        }
    }

    fun reqInterestUsers(followUsersLD: MutableLiveData<List<InterestUserBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqInterestUsers(it)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<List<InterestUserBean>>(this) {
                    override fun onSuccess(result: List<InterestUserBean>, json: String?) {
                        followUsersLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followUsersLD.value = null
                    }

                })
        }
    }


    fun reqRecommendUsers(
        index: Int,
        size: Int,
        followUsersLD: MutableLiveData<List<RecommendUserBean>>
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqRecommendUsers(it, index, size)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<List<RecommendUserBean>>(this) {
                    override fun onSuccess(result: List<RecommendUserBean>, json: String?) {
                        followUsersLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followUsersLD.value = null
                    }

                })
        }
    }

    fun reqNoCardUser(userId: Int, followUsersLD: MutableLiveData<Boolean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqNoCareUsers(it, userId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<Boolean>(this) {
                    override fun onSuccess(result: Boolean, json: String?) {
                        followUsersLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        followUsersLD.value = false
                    }

                })
        }
    }

    /**
     * 讲坛主页
     */
    fun reqPulpitHome(id: Long, pulpit: MutableLiveData<PulpitInfoBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqPulpitHome(it, id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<PulpitInfoBean>(this) {
                    override fun onSuccess(result: PulpitInfoBean, json: String?) {
                        pulpit.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        pulpit.value = null
                    }

                })
        }
    }

    /**
     * 讲坛课程列表
     */
    fun reqPulpitList(pulId: Long, pulpit: MutableLiveData<BasicListBean<PulpitListBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqPulpitList(it, pulId, Int.MAX_VALUE.toLong())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<BasicListBean<PulpitListBean>>(this) {
                    override fun onSuccess(result: BasicListBean<PulpitListBean>, json: String?) {
                        pulpit.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        pulpit.value = null
                    }

                })
        }
    }

    /**
     * 讲坛课程详情
     */
    fun reqPulCourse(courseId: Long, pulpit: MutableLiveData<PulCourseBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqPulCourse(it, courseId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<PulCourseBean>(this) {
                    override fun onSuccess(result: PulCourseBean, json: String?) {
                        pulpit.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        pulpit.value = null
                    }

                })
        }
    }

    /**
     * 解锁讲坛
     */
    fun reqUnlockPulpit(fid: Long, pulpit: MutableLiveData<Boolean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java)
                .reqUnlockPulpit(it, fid)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<Any>(this) {
                    override fun onSuccess(result: Any, json: String?) {
                        pulpit.value = true
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        pulpit.value = false
                    }

                })
        }
    }

    /**
     * 课程视频学习进度保存
     */
    fun reqSaveProgress(
        fid: Long,
        offset: Long,
        videoSaveProgress: Long,
        pulpit: MutableLiveData<Boolean>
    ) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqSaveProgress(it, fid, offset, videoSaveProgress)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : APIResponse<Boolean>(this) {
                    override fun onSuccess(result: Boolean, json: String?) {
                        pulpit.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        pulpit.value = false
                    }

                })
        }
    }

    fun reqAddFollow(thirdId: Long, apiResponse: APIResponse<String>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqAddFollow(it, 1, thirdId)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(apiResponse)
        }
    }

    fun reqAddFollow2(thirdId: Long, apiResponse: APIResponse<FollowSuccBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqAddFollow2(it, 1, thirdId)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(apiResponse)
        }
    }

    fun reqCancelFollow(thirdId: Long, apiResponse: APIResponse<String>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqCancelFollow(it, 1, thirdId)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(apiResponse)
        }
    }

    fun reqCancelFollow2(thirdId: Long, apiResponse: APIResponse<FollowSuccBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqCancelFollow2(it, 1, thirdId)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(apiResponse)
        }
    }

    fun reqAddLike(thirdId: Int, type: Int, apiResponse: APIResponse<Boolean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(BaseApi::class.java)
                .reqAddSupport(it, type, thirdId)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(apiResponse)
        }
    }

    fun reqCancelLike(thirdId: Int, type: Int, apiResponse: APIResponse<Boolean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(BaseApi::class.java)
                .reqCancelSupport(it, type, thirdId)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(apiResponse)
        }
    }

    fun reqAddCollect(thirdId: Long, type: Int, apiResponse: APIResponse<Boolean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(BaseApi::class.java)
                .reqAddCollection(it, type, thirdId)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(apiResponse)
        }
    }

    fun reqCancelCollect(thirdId: Long, type: Int, apiResponse: APIResponse<Boolean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(BaseApi::class.java)
                .reqCancelCollection(it, type, thirdId)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(apiResponse)
        }
    }

    fun reqDefSearchTopics(topicListLD: MutableLiveData<List<ReleaseTopicBean.DataBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqDefSearchTopics(it)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<List<ReleaseTopicBean.DataBean>>(this) {
                    override fun onSuccess(result: List<ReleaseTopicBean.DataBean>, json: String?) {
                        topicListLD.value = result
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                        topicListLD.value = null
                    }

                })
        }
    }

    fun reqStatistics(courId: Long) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                .getApi(CommunityApi::class.java)
                .reqStatistics(it, courId)
                .subscribeOn(Schedulers.newThread())
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<Any>(this) {
                    override fun onSuccess(result: Any, json: String?) {
                    }

                    override fun onFailed(message: String?) {
                        super.onFailed(message)
                    }

                })
        }
    }
    //话题分类列表  type 0选择话题 1全部话题
    fun requestTopicTagList(type: Int,listData: MutableLiveData<List<TopicClassifyBean>>) {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).requestTopicTagList(type)
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<List<TopicClassifyBean>>(this) {
                    override fun onSuccess(t: List<TopicClassifyBean>, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        listData.value = null
                    }
                })
    }
    //根据话题名搜索话题
    fun requestSearchTopicByName(searchKey:String,type:Int,listData: MutableLiveData<List<SearchTopicBean>>) {
            MyHttpUtil.getInstance().getApi(CommunityApi::class.java).requestSearchTopic(searchKey,type)
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<List<SearchTopicBean>>(this) {
                    override fun onSuccess(t: List<SearchTopicBean>, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        listData.value = null
                    }
                })
    }

    //根据话题名搜索话题
    fun requestHomepageData(listData: MutableLiveData<HomePageBean>) {
        MyHttpUtil.getInstance().getApi(CommunityApi::class.java).requestHomePageData()
                ?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribe(object : APIResponse<HomePageBean>(this) {
                    override fun onSuccess(t: HomePageBean, json: String) {
                        listData.value = t
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        listData.value = null
                    }
                })
    }
}